﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Jumpton.SFA.Server.BusinessRule.MasterData;
using Jumpton.SFA.Server.Entity;

/// <summary>
/// 税率维护
/// Update By:Steven Jiang
/// Update Time:2008-12-10 14:14
/// Description:
///     修改税率维护在一个页面。
/// </summary>
public partial class masterdata_Taxes_Type_List : PageBase
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            GetTaxesBind();
        }
    }

    public void GetTaxesBind()
    {

        GridPager1.DataSource = TaxesManager.GetTaxesTable(MyStateManager.DomainID);
        GridPager1.GridView = GridView1;
        GridPager1.BindData();
        GridPager1.Visible = GridView1.Rows.Count > 0;
       

        Button1_Click(null,null);
    }
    
    /// <summary>
    /// 保存方法
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnSave_Click(object sender, EventArgs e)
    {
        string LogContent = "";
        try
        {
            TAXES taxes = null;
            TaxesManager manager = new TaxesManager();
            TextBox ctl = DetailsView1.FindControl("txtName") as TextBox;

            if (DetailsView1.CurrentMode == DetailsViewMode.Insert)
            {
                taxes = new TAXES();
                taxes.CREATETIME = long.Parse(Jumpton.SFA.Server.BusinessRule.BusinessLogic.GetDateTimeString(DateTime.Now));
            }
            else
            {
                taxes = manager.Load(int.Parse(GridView1.SelectedDataKey.Value.ToString()));
            }
            taxes.DESCRIPTION = ctl.Text;
            ctl = DetailsView1.FindControl("txtTax") as TextBox;
            taxes.TAXES1 = Decimal.Parse(ctl.Text);

            ctl.Dispose();
            ctl = null;

            CheckBox cbxValid = DetailsView1.FindControl("chkValid") as CheckBox;
            taxes.ACTIVE = cbxValid.Checked;
            taxes.DOMAINID = MyStateManager.DomainID;
            taxes.LAST_UPDATER = MyStateManager.Name;//取到当前登陆用户名
            taxes.LAST_UPDATE_TIME = long.Parse(Jumpton.SFA.Server.BusinessRule.BusinessLogic.GetDateTimeString(DateTime.Now));
          
            manager.Save(taxes);

            LogContent += "税率值：" + taxes.TAXES1;
            LogContent += " 税率描述：" + taxes.DESCRIPTION;
            LogContent += " 是否有效：" + taxes.ACTIVE;
            LogContent += " 最后更新人：" + taxes.LAST_UPDATER;
            LogContent += " 最后更新时间：" + taxes.LAST_UPDATE_TIME;

            Jumpton.SFA.Server.BusinessRule.System.EventManager.CreateNewEvent(
             Jumpton.SFA.Server.BusinessRule.System.LogType.Info_Change, this.MyStateManager.Name,
             this.MyStateManager.UserID, LogContent);
            //ShowMessage("保存成功");
            if (DetailsView1.CurrentMode == DetailsViewMode.Insert)
            {
                GetTaxesBind();
            }
            else
            {
                Response.Redirect(Request.RawUrl.ToString());
            }
        }
        catch (Exception ex)
        {
            Jumpton.SFA.Server.BusinessRule.System.EventManager.CreateNewEvent(
           Jumpton.SFA.Server.BusinessRule.System.LogType.Info_Change, this.MyStateManager.Name,
           this.MyStateManager.UserID, "税率维护-异常："+ex.Message);
            ShowMessage(ex.Message);
            return;
        }

    }


    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        DetailsView1.ChangeMode(DetailsViewMode.Edit);
        DetailsView1.DataBind();
        
    }
    protected void DetailsView1_DataBound(object sender, EventArgs e)
    {
        CheckBox chk = DetailsView1.FindControl("chkValid") as CheckBox;

        if (DetailsView1.DataItem == null)
        {
            chk.Checked = true;
        }
    }


    //新增
    protected void Button1_Click(object sender, EventArgs e)
    {

        DetailsView1.ChangeMode(DetailsViewMode.Insert);
        DetailsView1.DataBind();

    }

    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        GetTaxesBind();
    }
}
